Hi everyone,
I tried to calculate and plot moving average of close price of external data loaded into quantopian. I managed to plot close price. After tried a few times on mavg
and SimpleMovingAverage
, however, I still cannot get it right. I have no idea where went wrong. Could you help?
Thanks a lot!
the format of my data:
Date,symbol,open,high,low,close,volume
2010/04/16,IF,3450,3488,3413.2,3415.6,48988
2010/04/19,IF,3396,3396.2,3168.4,3197.8,109728
Here is my code:
from quantopian.pipeline.factors import SimpleMovingAverage
def initialize(context):
fetch_csv('https://myfile/data.csv',
date_column = 'Date',
date_format = '%Y/%m/%d',
pre_func = preview,
symbol='IF')
context.stock = symbol('IF')
def preview(df):
log.info(df.close.head())
return df
def handle_data(context, data):
current_price = data['IF']['close']
sma_10 = SimpleMovingAverage(inputs=data['IF']['close'], window_length=10)
sma_30 = SimpleMovingAverage(inputs=data['IF']['close'], window_length=30)
sma_20 = data['IF']['close'].mavg(20)
sma_50 = data['IF']['close'].mavg(50)
record(MA1 = sma_10, MA2 = sma_50, Price = current_price)